package client_listen_update_files;

import java.io.File;
import net.contentobjects.jnotify.JNotify;
import net.contentobjects.jnotify.JNotifyListener;


/**
 * listen to OS for changes in shared folder
 */

public class FolderListener implements Runnable {	
	private String listeningFolder=null;
	//indicates if we need to stop
	private volatile boolean stop;
	
	/**
	 * d.cont, listening to dropBox path
	 */	
	public FolderListener() {
		listeningFolder=ConfigManager.get().getDropBoxPath();
		stop=true;
	}
	/*
	 * listen to a folder 
	 */
	private void listenToFolder(){
		stop=false;
				
		try {
			 // watch mask, specify events you care about,
		    // or JNotify.FILE_ANY for all events.
		    int mask = JNotify.FILE_CREATED  | 
		               JNotify.FILE_DELETED  | 
		               JNotify.FILE_MODIFIED | 
		               JNotify.FILE_RENAMED;
		    
		    // watch subtree
		    boolean watchSubtree = true;
		
		 // add actual watch
		    int watchID = JNotify.addWatch(listeningFolder, mask, watchSubtree, new JNotifyListener(){
		    			 
				@Override
				public void fileCreated(int wd, String rootPath, String name) {
					createFile(rootPath,name);

				}

				@Override
				public void fileDeleted(int wd, String rootPath, String name) {
					deleteFile(rootPath,name);				

				}

				@Override
				public void fileModified(int wd, String rootPath, String name) {

						modifyFile(rootPath,name);
				}

				@Override
				public void fileRenamed(int wd, String rootPath, String oldName,String newName) {			
					renamed(rootPath,oldName,newName);					
				}
		    	
		    });
		   // while (!stop){
		   	Thread.sleep(1000000);
		   // }
			////JNotify.removeWatch(watchID);
			
		} catch (Exception e) {
			System.out.println("Error: " + e.toString());	
		}		

	}
	/**
	 * stop listening to a folder
	 */
	public void stop(){
		stop=true;
	}
	/*
	 * modify file event occurred, notify server
	 * @param dirPath root path
	 * @param name file's name
	 */
	private void modifyFile(String dirPath,String name) {
		
		File file=new File(dirPath+System.getProperty("file.separator")+name);
		//check if not a dir
		if (!file.isDirectory()){
			//send modified file to server
			ServerCommManager.getCommManager().sendFile(file,name);
		}

	}
	
	/*
	 * delete event occurred, notify server
	 * @param path root path
	 * @param name file name
	 */
	private void deleteFile(String path,String name) {
		//notify server file or folder got deleted
		ServerCommManager.getCommManager().deleteFile(name);
	}
	
	/*
	 * create event occurred, notify server
	 * @param dirPath root path
	 * @param name file name
	 */
	private void createFile(String dirPath,String name) {		
		File file=new File(dirPath+System.getProperty("file.separator")+name);		
		//notify server file or folder got created
		ServerCommManager.getCommManager().sendFile(file,name);		
		
	}
	
	/*
	 * rename event occurred, notify server
	 * @param path root path
	 * @param oldName old file name
	 * @param newName new file name
	 */
	private void renamed(String path,String oldName,String newName){
		//notify server file or folder got renamed
		ServerCommManager.getCommManager().rename(oldName,newName);		
	}

	@Override
	public void run() {
		listenToFolder();
		
	}
}